//
//  AppsJsonParser.m
//  SandBayCinema
//
//  Created by Rayco on 12-10-24.
//  Copyright (c) 2012年 Apps123. All rights reserved.
//

#import "AppsJsonParser.h"

@implementation AppsJsonParser

+ (NSMutableArray *)parseImageUrlFromPhotoGalleryList:(NSMutableArray *)list {
    if(!list){
        return nil;
    }
    NSMutableArray *data = [NSMutableArray array];
    for(int i = 0;i < [list count]; i++){
        NSDictionary *listItem = [list objectAtIndex:i];
        if(listItem){
            NSArray *detailVOList = [listItem objectForKey:@"photoGalleryDetailVOList"];
            if(detailVOList){
                for(int j = 0;j < [detailVOList count]; j++){
                    if(j == 0){
                        NSDictionary *VOListItem = [detailVOList objectAtIndex:j];
                        if(VOListItem){
                            [data addObject:VOListItem];
                        }
                    }
                }
            }
        }
    }
    return data;
}

+ (NSMutableArray *)parseImageUrlFromHomePage:(NSMutableDictionary *)dataInfo {
    if(!dataInfo){
        return nil;
    }
    NSMutableArray *data = [NSMutableArray array];
    
    for(int i = 0; i < 8; i++){
        NSString *pic = [NSString stringWithFormat:@"pic%d",i + 1];
        NSString *imageUrl = [dataInfo objectForKey:pic];
        if(imageUrl && ![imageUrl isEqualToString:@""]){
            
            NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
            [imageDic setValue:imageUrl forKey:@"ImageUrl"];
            
            [data addObject:imageDic];
        }
    }
    return data;
}

+ (NSMutableArray *)parseTitleAndUrlFromPhotoGalleryList:(NSMutableArray *)list {
    if(!list){
        return nil;
    }
    NSMutableArray *data = [NSMutableArray array];
    for(int i = 0;i < [list count]; i++){
        NSDictionary *listItem = [list objectAtIndex:i];
        if(listItem){
            NSArray *detailVOList = [listItem objectForKey:@"photoGalleryDetailVOList"];
            if(detailVOList){
                for(int j = 0;j < [detailVOList count]; j++){
                    NSDictionary *VOListItem = [detailVOList objectAtIndex:j];
                    if(VOListItem){
                        [data addObject:[NSDictionary dictionaryWithObjectsAndKeys:[VOListItem objectForKey:@"pictureURL"],@"ImageUrl",[VOListItem objectForKey:@"pictureTitle"],@"Title",[VOListItem objectForKey:@"id"],@"id", nil]];
                    }
                }
            }
        }
    }
    return data;
}

+ (NSMutableDictionary *)parseListDataFromPhotoInfoTabList:(NSMutableDictionary *)data title:(NSString *)title index:(NSInteger)index {
    NSMutableDictionary *dataSource = [NSMutableDictionary dictionary];
    
    NSMutableArray *dataList = [NSMutableArray array];
 
    NSMutableArray *pageList = [data objectForKey:@"pageList"];
    if(pageList){
        for(int i = 0;i < [pageList count]; i++){
            NSMutableDictionary *pageListItem = [pageList objectAtIndex:i];
            [dataList addObject:pageListItem];
        }
    }
    [dataSource setValue:dataList forKey:@"DataList"];
    [dataSource setValue:title forKey:@"Title"];
    [dataSource setValue:[data objectForKey:@"pageCount"] forKey:@"pageCount"];
    [dataSource setValue:[data objectForKey:@"current"] forKey:@"current"];
    
    return dataSource;
}

+ (NSMutableArray *)parseListDataFromPhotoInfoTabCatagory:(NSMutableArray *)data {
    NSMutableArray *dataList = [NSMutableArray array];
    if(data && [data count] > 0){
        [dataList addObjectsFromArray:data];
    }    
    return dataList;
}

+ (NSMutableDictionary *)parseListDataFromPhotoInfoTabList:(NSMutableDictionary *)data max:(NSInteger)max title:(NSString *)title index:(NSInteger)index {
    NSMutableDictionary *dataSource = [NSMutableDictionary dictionary];
    
    NSMutableArray *dataList = [NSMutableArray array];
    
    NSMutableArray *pageList = [data objectForKey:@"pageList"];
    if(pageList){
        NSInteger count = [pageList count] > max ? max : [pageList count];
        for(int i = 0;i < count; i++){
            NSMutableDictionary *pageListItem = [pageList objectAtIndex:i];
            [dataList addObject:pageListItem];
        }
    }
    [dataSource setValue:dataList forKey:@"DataList"];
    [dataSource setValue:title forKey:@"Title"];
    [dataSource setValue:[data objectForKey:@"count"] forKey:@"count"];
    [dataSource setValue:[data objectForKey:@"current"] forKey:@"current"];
    
    NSMutableDictionary *result = [NSMutableDictionary dictionary];
    [result setValue:dataSource forKey:[NSString stringWithFormat:@"%d",index]];
    
    return result;
}

+ (NSMutableArray *)parseImageListFromAboutMerchant:(NSMutableDictionary *)data {
    NSMutableArray *imageList = [NSMutableArray array];
    if(data){
        for(int i = 0;i < 5; i++){
            NSString *pic = [data objectForKey:[NSString stringWithFormat:@"pic%d",i + 1]];
            if(pic && ![pic isEqualToString:@""]){
                [imageList addObject:[NSDictionary dictionaryWithObjectsAndKeys:pic,@"ImageUrl", nil]];
            }
        }
    }
    return imageList;
}

+ (NSMutableDictionary *)parseMerchantInfoFromAboutMerchant:(NSMutableDictionary *)data {
    NSMutableDictionary *dataInfo = [NSMutableDictionary dictionary];
    if(data){
//        "email":"swwhz2008@126.com",
//        "fax":"02034737921",
//        "name":"沙湾文化中心",
//        "phone":"02034737921",
//        "address":"广州市番禺区沙湾镇大巷涌路66号沙湾文化中心",
//        "postCode":"511483",
//        "province":"0",
//        "servicehours":"",
//        "website":""
        [dataInfo setValue:[data objectForKey:@"email"] forKey:@"email"];
        [dataInfo setValue:[data objectForKey:@"fax"] forKey:@"fax"];
        [dataInfo setValue:[data objectForKey:@"name"] forKey:@"name"];
        [dataInfo setValue:[data objectForKey:@"phone"] forKey:@"phone"];
        [dataInfo setValue:[data objectForKey:@"address"] forKey:@"address"];
        [dataInfo setValue:[data objectForKey:@"postCode"] forKey:@"postCode"];
        [dataInfo setValue:[data objectForKey:@"website"] forKey:@"website"];
       
        [dataInfo setValue:[data objectForKey:@"detailDescription"] forKey:@"detailDescription"];
    }
    return dataInfo;
}

+ (NSMutableDictionary *)parsePhotoTabInfoDetailFromPhotoTabInfo:(NSMutableDictionary *)data {
    
    
    return data;
}

+ (NSMutableArray *)parseImagesFromSingleBranchesEnquiry:(NSMutableDictionary *)data {
    NSMutableArray *dataInfo = [NSMutableArray array];
    for(int i = 0;i < 5; i++){
        NSString *pic = [NSString stringWithFormat:@"pic%d",i + 1];
        NSString *picValue = [data objectForKey:pic];
        if(picValue && ![picValue isEqualToString:@""]){
            [dataInfo addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[data objectForKey:pic],@"ImageUrl", nil]];
        }
    }
    
    return dataInfo;
}

+ (NSMutableArray *)parseQRPlatformList:(NSMutableDictionary *)data {
    NSMutableArray *dataInfo = [NSMutableArray array];
    
    if(data){
        NSMutableArray *platformList = [data objectForKey:@"platformList"];
        if(platformList){
            for(int i = 0;i < [platformList count]; i++){
                NSDictionary *platformDic = [platformList objectAtIndex:i];
                [dataInfo addObject:platformDic];
            }
        }
    }
    return dataInfo;
}

+ (NSString *)getDownloadLink:(NSMutableArray *)data {
    if(!data || [data count] == 0){
        return @"";
    }
    NSString *link = @"";
    for(int i = 0;i < [data count]; i++){
        NSDictionary *dic = [data objectAtIndex:i];
        if(dic){
            NSString *platformStoreCode = [dic objectForKey:@"platformStoreCode"];
            if(platformStoreCode && [platformStoreCode isEqualToString:@"005"]){
                NSString *QRCodeLinks = [dic objectForKey:@"QRCodeLinks"];
                if(![QRCodeLinks isKindOfClass:[NSNull class]] && QRCodeLinks && ![QRCodeLinks isEqualToString:@""]){
                    if(![link isEqualToString:@""])
                        link = [NSString stringWithFormat:@"%@ 下载沙湾iOS版本:%@",link,QRCodeLinks];
                    else
                        link = [NSString stringWithFormat:@"\n下载沙湾iOS版本:%@",QRCodeLinks];
                }
            }
            if(platformStoreCode && [platformStoreCode isEqualToString:@"007"]){
                NSString *QRCodeLinks = [dic objectForKey:@"QRCodeLinks"];
                if(![QRCodeLinks isKindOfClass:[NSNull class]] && QRCodeLinks && ![QRCodeLinks isEqualToString:@""]){
                    if(![link isEqualToString:@""])
                        link = [NSString stringWithFormat:@"%@ 下载沙湾Android版本:%@",link,QRCodeLinks];
                    else
                        link = [NSString stringWithFormat:@"\n下载沙湾Android版本:%@",QRCodeLinks];

                }
            }
        }
    }
    return link;
}

@end
